-- UIAirShipFix
-- Create by cheny3
-- 飞艇维修界面

--UIAirShipFix继承自Layer
UIAirShipFix = class("UIAirShipFix", function()
    return cc.Layer:create();
end);

function UIAirShipFix.create(title, desc, icon, okText, okFunc, extraPara)
    return UIAirShipFix.new(title, desc, icon, okText, okFunc, extraPara);
end

-- 构造函数
function UIAirShipFix:ctor(title, desc, icon, okText, okFunc, extraPara)
    self:setName("UIAirShipFix");

    -- 初始化数据
    self.title = title;
    self.desc = desc;
    self.icon = icon;
    self.okText = okText or getLocStr("btn_text_ok");
    self.okFunc = okFunc;
    self.extraPara = extraPara;

    -- 初始化
    local node = cc.CSLoader:createNode("layout/workshop/StrengthenDetail.csb");
    self:addChild(node);
    self.node = node;

    -- 注册点击事件
    self:registerTouchEvent();

    self.costParentNode = findChildByName(self.node, "CT/cost");
    self.originCostParentPosX = self.costParentNode:getPositionX();
    self.originCostParentPosY = self.costParentNode:getPositionY();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then

        elseif ev == "enter" then

        end
    end);
end

-- 注册点击事件
function UIAirShipFix:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("ship_return");
            UIMgr.getCurrentScene():removeFormByName("UIAirShipFix");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确认按钮点击事件
    local btnOk = findChildByName(self.node, "CT/strengthen");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            if self.okFunc and type(self.okFunc) == "function" then
                self.okFunc();
                UIMgr.getCurrentScene():removeFormByName("UIAirShipFix");
            end
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(self.okText);

    -- 是否置灰按钮
    if self.extraPara and self.extraPara["okBtnEnable"] ~= nil then
        btnOk:setTouchEnabled(self.extraPara["okBtnEnable"]);
        btnOk:setBright(self.extraPara["okBtnEnable"]);
    end
end

-- 界面重绘
function UIAirShipFix:redraw()
    -- 设置标题样式
    local titleLabel = findChildByName(self.node, "CT/template/title");
    titleLabel:setString(self.title);
    TextStyleM.setTitleStyle(titleLabel);

    -- 道具描述1
    local desc1Label = findChildByName(self.node, "CT/desc1");
    TextStyleM.setTextStyle(desc1Label,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(desc1Label, self.desc);

    -- 图标
    local item = findChildByName(self.node, "CT/item");
    local icon = item:getChildByName("icon");
    icon:loadTexture(self.icon);
    if self.extraPara then
        if self.extraPara["scale"] then
            icon:setScale(self.extraPara["scale"]);
        end
        if self.extraPara["flippedX"] then
            icon:setFlippedX(true);
        end
        if self.extraPara["offset"] then
            icon:setPositionX(icon:getPositionX() + self.extraPara["offset"][1]);
            icon:setPositionY(icon:getPositionY() + self.extraPara["offset"][2]);
        end
    end

    -- 底框
    local frame = item:getChildByName("frame");
    frame:loadTexture(getWorkshopStrengthenIconPath("bg2"));

    -- 花费
    self:initCost();

    -- 调整高度
    self:adjustHeight();
end

-- 花费说明
function UIAirShipFix:initCost()
    local costLabel = findChildByName(self.node, "CT/cost");
    TextStyleM.setTextStyle(costLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false);
    if self.extraPara and self.extraPara["costDesc"] then
        applyString(costLabel, self.extraPara["costDesc"]);
    end
end

-- 调整高度
function UIAirShipFix:adjustHeight()
    local costLabel = findChildByName(self.node, "CT/cost");
    local lineNum = costLabel:getVirtualRenderer():getStringNumLines();

    local delta = (3 - lineNum) * TextStyleM.TEXT_SIZE_SMALL;
    if delta == 0 then
        return;
    end

    costLabel:setPositionY(costLabel:getPositionY() + delta / 2);

    local btnBuy = findChildByName(self.node, "CT/strengthen");
    btnBuy:setPositionY(btnBuy:getPositionY() + delta);

    local bg = findChildByName(self.node, "CT/template/bg");

    local bgDelta = delta / bg:getScaleY();
    local bg3 = findChildByName(self.node, "CT/template/bg/bg3");
    bg3:setPositionY(bg3:getPositionY() + bgDelta );
    local bg4 = findChildByName(self.node, "CT/template/bg/bg4");
    bg4:setPositionY(bg4:getPositionY() + bgDelta);
end

-- 适配
function UIAirShipFix:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end


